package cn.edu.sctu.sctu_lysd.net;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Created by wangfuxin on 2016/10/24.
 */

public class RetrofitHelper {

    private static RetrofitHelper instance;
    private static Retrofit retrofit;
    public static String BASE_URL = "http://yunwei.sctu.edu.cn:18080";
    private static final int DEFAULT_TIMEOUT = 10;//超时时长，单位：秒

    private RetrofitHelper() {

    }

    /**
     * 单例
     *
     * @return
     */
    public static RetrofitHelper getInstance() {
        if (instance == null) {

            instance = new RetrofitHelper();

            OkHttpClient.Builder okHttpBuilder = new OkHttpClient.Builder();
            okHttpBuilder.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);

            //初始化 retrofit
            retrofit = new Retrofit.Builder()
                    .client(okHttpBuilder.build())
                    .addConverterFactory(ScalarsConverterFactory.create())
                    .addConverterFactory(GsonConverterFactory.create())
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                    .build();
        }
        return instance;
    }


    /**
     * 创建数据请求服务
     */
    public Object getService(Class clz) {

        return retrofit.create(clz);

    }


}
